<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_Group extends DB{
    private $rowCount=0;
    public $table_name="tb_group";
    function M_Group(){
		parent::__construct();
	//$this->dbConn=$this->getDB();
    }
     
    function loadData(){
        
        $sqlscript= "SELECT id, name ,status ";
        $sqlscript.= "FROM ". $this->table_name;
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
	function loadPermissionScreen($gid=0,$t='MA'){
		$sql ="";
		if ($gid==0){
			$sql.=" SELECT  sp.* ,sp.v as isView, sp.a as isAdd, sp.e as isEdit, sp.d as isDel ,sp.col1 as isCol1
					FROM tb_screen_permission sp
					WHERE 1=1 AND sp.type='".$t."'";
		}else {
			$sql .=" SELECT  sp.id , sp.name, ifnull(gp.v,0) as v, ifnull( gp.a ,0) as a 
				,ifnull(gp.e,0) as e, ifnull(gp.d ,0) as d , ifnull(gp.col1,0) as col1
				,ifnull(gp.col2,0) as col2
				,sp.v as isView, sp.a as isAdd, sp.e as isEdit, sp.d as isDel , sp.col1 as isCol1
				FROM tb_screen_permission sp
				LEFT JOIN tb_group_permission gp on gp.sid = sp.id and gp.gid=".$gid."
				WHERE 1=1  AND sp.type='".$t."'";
		}
		
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}   

    function getRowCount(){
        
        return $this->rowCount;
        
    }	
    function insertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript("SELECT name,status , createdate, createby FROM ". $this->table_name." where 1=2 ", $val);
			$this->ExecuteData($sqlInsert);
			$id = $this->getLastID();
			return $id;
		}
    }
	function InsertPermission($ar){
		if (isset($ar)){
			$sqlInsert=$this->InsertSqlScript("SELECT gid, sid, v, a, e, d, col1 FROM tb_group_permission  where 1=2", $ar);
			$this->ExecuteData($sqlInsert);
		}
	}
	function DeletePermission($gid){
		$sql = "DELETE FROM tb_group_permission WHERE gid=".$gid;
		$this->ExecuteData($sql);
	}
	 
    function UpdateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT name,status , updatedate, updateby FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    } 
	function delePermission($gid){
	        if(isset($gid)){
			$sqlDelete = "Delete FROM tb_group_permission where gid=".$gid ;
			$this->ExecuteData($sqlDelete);
			return 1;
            }
	}
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM ". $this->table_name." where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			
			$sqlDelete = "Delete FROM tb_group_permission where gid=".$id ;
			$this->ExecuteData($sqlDelete);
			
			return 1;
            }
    }
	function getType(){
	 
		$sqlscript= "select id, name from tb_product_type where status = 'Y' Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	
	}
    
}
?>
